<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">修改部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit?'修改部门':'添加子部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="clForm"
      >
        <!-- 父传子 -->
        <dept-dialog :id="curId" ref="myAddRef" :origin-list="originList" :is-edit="isEdit" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { delDepartment, getDepartment } from '@/api/departments'
import { tranDateToTreeDate } from '@/utils/index'
import DeptDialog from './deptDialog.vue'
export default ({
  components: {
    DeptDialog
  },
  data() {
    return {
      isEdit: false, // true为修改部门 || false为添加子部门
      curId: '',
      showDialog: false,
      list: [
        {
          name: '财务部',
          manager: '刘备',
          children: [
            {
              name: '财务核算部',
              manager: '张飞',
              children: []
            },
            {
              name: '税务核算部',
              manager: '关羽',
              children: []
            }
          ]
        }],
      originList: []
      // defaultProps: {
      //   children: 'children',
      //   label: 'name'
      // }
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    // 关闭diaLog重置表单
    clForm() {
      // 调用子组件的发方法
      this.$refs.myAddRef.resetForm()
    },
    // 查询企业部门列表
    async getDepartmentList() {
      const res = await getDepartment()
      // 删除第一条数据
      res.data.depts.shift()
      this.originList = res.data.depts.map(item => {
        return { id: item.id, name: item.name, pid: item.pid, code: item.code }
      })
      // console.log(res.data.depts)
      // console.log(tranDateToTreeDate(res.data.depts))
      this.list = tranDateToTreeDate(res.data.depts)
    },
    // 添加部门
    hAdd(id) {
      this.curId = id
      this.showDialog = true
      this.isEdit = false
    },
    hSuccess() {
      // 重新请求渲染
      this.getDepartmentList()
      this.showDialog = false
    },
    // 修改部门
    hEdit(id) {
      this.curId = id
      this.showDialog = true
      this.isEdit = true
      this.$nextTick(() => {
        this.$refs.myAddRef.loadDepartmentById()
      })
    },
    // 删除部门
    hDel(id) {
      this.$confirm('你确定要删除这条数据吗').then(async() => {
        const res = await delDepartment(id)
        console.log(res)
        this.$message.success(res.message)
        this.getDepartmentList()
      }).catch(err => console.log(err))
    }
  }
})
</script>
